package com.edfu.core.identitymgt.util;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import com.edfu.core.identitymgt.persistence.entity.RoleEntity;
import com.edfu.core.identitymgt.persistence.entity.UserEntity;

public class IdentityMgtEntityUtil {
	
	public static UserEntity getUserEntity(String userId, EntityManager entityManager) {
		Query q = entityManager.createQuery("select u from UserEntity u where userId = :userId");
		q.setParameter("userId", userId);
		return (UserEntity)q.getResultList().get(0);
	}
	
	public static RoleEntity getRoleEntity(String roleName, EntityManager entityManager) {
		Query q = entityManager.createQuery("select e from RoleEntity e where e.name = :roleName");
		q.setParameter("roleName", roleName);
		List<?> resultList = q.getResultList();
		if (resultList == null || resultList.size() == 0)
			return null ;
		else 
			return (RoleEntity)q.getResultList().get(0);
	}
	

}
